<script setup>
import { ref, onMounted } from 'vue'
import {Search, Refresh, View, Download} from '@element-plus/icons-vue'
import axios from "axios";
import {ElMessage} from "element-plus";

// 查询参数
const queryParams = ref({
  userAccount: '',
  temperatureStatus:'',
  beginTemperature: '0.0',
  endTemperature: '0.0',
})

// 体温状态选项
const temperatureStatusOptions = ref([
  { value: 1, label: '正常' },
  { value: 2, label: '低烧' },
  { value: 3, label: '高烧' },
  { value: 4, label: '低温' }
])

// 表格数据
const tableData = ref([])
const total = ref(0)
const pageSize = ref(10)
const pageNum = ref(1)

// 记录对话框相关
const dialogVisible = ref(false)
const recordData = ref([])
const currentUser = ref(null)

// 获取表格数据
const fetchData = async () => {
  try {
    //实现查询逻辑
    axios.get(`/health/getHealth/7/${pageNum.value}/${pageSize.value}`).then((response) => {
      console.log(response.data.data);
      total.value = response.data.total;
      tableData.value = response.data.data;
    })
    ElMessage.success('查询成功')
  } catch (error) {
    ElMessage.error('获取数据失败: ' + error.message)
  }
}

// 综合查询
const handleQuery = () => {
  try {
    axios.post(`/health/getHealthData/${pageNum.value}/${pageSize.value}`,queryParams.value).then(response => {
      total.value = response.data.total;
      tableData.value = response.data.data;
    })
  } catch (error) {
    ElMessage.error('查询失败')
  }
}

// 重置
const resetQuery = () => {
  queryParams.value = {
    userAccount: '',
    temperatureStatus:'',
    beginTemperature: '0.0',
    endTemperature: '0.0',
  }
  fetchData()
}

// 分页大小变化
const handleSizeChange = (val) => {
  pageSize.value = val;
  fetchData()
}
// 当前页变化
const handleCurrentChange = (val) => {
  pageNum .value= val
  fetchData()
}
// 查看记录
const handleViewRecord = (row) => {
  // currentUser.value = row
  //这里应该是查询用户体温明细
  // dialogVisible.value = true
}
// 导出数据
const exportData = () => {
  // axios.get(`/health//download/1/${currentPage.value}/${pageSize.value}`).then((response) => {
  //用同步请求而不是异步请求
  location.href = `http://localhost:8080/health/download/1/${pageNum.value}/${pageSize.value}`;
  ElMessage.success('导出成功');
}

// 根据体温获取状态
const getTemperatureStatus = (temp) => {
  const num = parseFloat(temp)
  if (num < 36.0) return { label: '低温', type: 'info' }
  if (num > 38.0) return { label: '高烧', type: 'danger' }
  if (num > 37.2) return { label: '低烧', type: 'warning' }
  return { label: '正常', type: 'success' }
}

// 根据体温获取样式类
const getTemperatureClass = (temp) => {
  const num = parseFloat(temp)
  if (num < 36.0) return 'low-temperature'
  if (num > 38.0) return 'high-temperature'
  if (num > 37.2) return 'slightly-high-temperature'
  return 'normal-temperature'
}

// 初始化加载数据
onMounted(() => {
  fetchData()
})
</script>

<template>
  <div class="temperature-container">
    <!-- 查询区域 -->
    <div class="query-area">
      <el-card shadow="hover">
        <el-form :model="queryParams" label-width="100px">
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="用户账号">
                <el-input
                    v-model="queryParams.userAccount"
                    placeholder="请输入用户账号"
                    clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="体温状态">
                <el-select
                    v-model="queryParams.temperatureStatus"
                    placeholder="请选择体温状态"
                    clearable
                >
                  <el-option
                      v-for="item in temperatureStatusOptions"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8" class="button-group">
              <el-button type="primary" @click="handleQuery">
                <el-icon><Search /></el-icon>
                查询
              </el-button>
              <el-button @click="resetQuery">
                <el-icon><Refresh /></el-icon>
                重置
              </el-button>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
    </div>

    <!-- 数据区域 -->
    <div class="data-area">
      <div class="header">
        <h3>用户血糖数据统计</h3>
        <el-button type="primary" @click="exportData" >导出数据</el-button>
      </div>
      <el-card shadow="hover">
        <el-table :data="tableData" border style=" width: 100%">
          <el-table-column prop="userAccount" label="用户账号" width="120">
            <template #default="{ row }">
              <div style="display: flex; align-items: center;">
                <span>{{ row.userAccount }}</span>
                <el-tag
                    :type="getTemperatureStatus(row.temperature).type"
                    size="small"
                    style="margin-left: 8px;"
                >
                  {{ getTemperatureStatus(row.temperature).label }}
                </el-tag>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="userNickname" label="用户昵称" width="180" />
          <el-table-column prop="temperature" label="体温(℃)" width="120">
            <template #default="{ row }">
              <span :class="getTemperatureClass(row.temperature)">
                {{ row.temperature }}
              </span>
            </template>
          </el-table-column>
          <el-table-column prop="temperatureLastMeasurementTime" label="最近测量时间" width="200" />
          <el-table-column label="操作" width="150">
            <template #default="{ row }">
              <el-button
                  type="primary"
                  size="small"
                  @click="handleViewRecord(row)"
              >
                <el-icon><View /></el-icon>
                查看记录
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>

    <!-- 分页区域 -->
    <div class="pagination-area">
      <el-pagination
          v-model:current-page="pageNum"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>

    <!-- 查看记录对话框 -->
    <el-dialog v-model="dialogVisible" title="体温测量记录" width="70%">
      <el-table :data="recordData" border>
        <el-table-column prop="measureTime" label="测量时间" width="180" />
        <el-table-column prop="temperature" label="体温(℃)" width="120">
          <template #default="{ row }">
            <span :class="getTemperatureClass(row.temperature)">
              {{ row.temperature }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="device" label="测量设备" />
        <el-table-column prop="remark" label="备注" />
      </el-table>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.temperature-container {
  padding: 20px;
}

.query-area {
  margin-bottom: 20px;
}

.data-area {
  margin-bottom: 20px;
}

.pagination-area {
  display: flex;
  justify-content: flex-end;
}

.button-group {
  display: flex;
  align-items: center;
  justify-content: flex-end;
}

/* 体温样式 */
.normal-temperature {
  color: var(--el-color-success);
}

.slightly-high-temperature {
  color: var(--el-color-warning);
}

.high-temperature {
  color: var(--el-color-danger);
  font-weight: bold;
}

.low-temperature {
  color: var(--el-color-info);
}
</style>